package com.nineclock.document.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nineclock.common.entity.PageResult;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.common.utils.BeanHelper;
import com.nineclock.document.dto.DocFileDto;
import com.nineclock.document.dto.DocFileHistoryDto;
import com.nineclock.document.mapper.DocumentHistoryMapper;
import com.nineclock.document.mapper.DocumentMapper;
import com.nineclock.document.pojo.DocFile;
import com.nineclock.document.pojo.DocFileHistory;
import com.nineclock.document.service.DocumentService;
import com.nineclock.system.dto.SysCompanyUserDTO;
import com.nineclock.system.feign.SysOrganizationFeign;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DocumentServiceImpl implements DocumentService {
    @Autowired
    DocumentMapper documentMapper;

    @Autowired
    SysOrganizationFeign organizationFeign;

    //分页查询文件列表
    @Override
    public PageResult<DocFileDto> QueryDocumentList(Integer page, Integer pageSize, String keyword) {

        LambdaQueryWrapper<DocFile> fileLambdaQw = new LambdaQueryWrapper<>();
        fileLambdaQw.eq(DocFile::getCompanyId, CurrentUserHolder.get().getCompanyId());
        if (keyword != null) {
            fileLambdaQw.like(DocFile::getName, keyword);
        }
        //创建page对象
        Page<DocFile> docFilePage = new Page<>();

        //分页查询
        docFilePage = documentMapper.selectPage(docFilePage, fileLambdaQw);
        //获取数据列表
        List<DocFile> records = docFilePage.getRecords();

        List<DocFileDto> docFileDtoList = BeanHelper.copyWithCollection(records, DocFileDto.class);

        for (DocFileDto docFileDto : docFileDtoList) {
            SysCompanyUserDTO companyUserDTO = organizationFeign.findCompanyUserById(docFileDto.getCreateUserId()).getData();
            docFileDto.setCreator(companyUserDTO);
            docFileDto.setUpdateUserName("nul");
        }

        return new PageResult(
                docFilePage.getTotal(),
                docFilePage.getPages(),
                docFileDtoList
        );
    }

    @Override
    public void update(Long id, Short status) {
        if (id == null || status == null) {
            throw new NcException(ResponseEnum.UPDATE_OPERATION_FAIL);
        }
        DocFile docFile = documentMapper.selectById(id);
        if (docFile != null) {
            docFile.setStatus(status);
        }
        documentMapper.updateById(docFile);
    }

    @Autowired
    DocumentHistoryMapper historyMapper;
    @Override
    public List<DocFileHistoryDto> QueryHistoryOperation(Long fileId) {
        if (fileId == null) {
            throw new NcException(ResponseEnum.ERROR);
        }
        LambdaQueryWrapper<DocFileHistory> FileHistoryLambdaQw = new LambdaQueryWrapper<>();
        FileHistoryLambdaQw.eq(DocFileHistory::getFileId, fileId);
        List<DocFileHistory> docFileHistoryList = historyMapper.selectList(FileHistoryLambdaQw);

        //获取操作人
        List<DocFileHistoryDto> fileHistoryDtoList = BeanHelper.copyWithCollection(docFileHistoryList, DocFileHistoryDto.class);
        for (DocFileHistoryDto docFileHistoryDto : fileHistoryDtoList) {
            SysCompanyUserDTO companyUserDTO = organizationFeign.findCompanyUserById(docFileHistoryDto.getOperateUserId()).getData();
            String userName = companyUserDTO.getUserName();
            docFileHistoryDto.setUserName(userName);
        }
        return fileHistoryDtoList;
    }
}
